package com.medical.acc.service.impl;

import com.medical.common.paginator.domain.PageBounds;
import com.medical.common.paginator.domain.PageList;
import com.medical.acc.mapper.PeopleOrganizationMapMapper;
import com.medical.acc.mapper.UserPeopleMapper;
import com.medical.acc.model.People;
import com.medical.acc.model.PeopleOrganizationMap;
import com.medical.acc.service.PeopleService;
import com.medical.acc.mapper.PeopleMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2016/10/10.
 */
@Service("peopleService")
public class PeopleServiceImpl implements PeopleService {
    @Autowired
    private PeopleMapper peopleMapper;
    @Autowired
    private PeopleOrganizationMapMapper peopleOrganizationMapMapper;
    @Autowired
    private UserPeopleMapper userPeopleMapper;




    public PageList<People> searchList(String query, int page, int pageSize) {
        return null;
    }

    public PageList<People> searchList(String query, Integer page, Integer pageSize, String sortColumn, String sortDir) {
        Map<String,Object> params = new HashMap<String,Object>();
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=20;
        }
        if(!StringUtils.isEmpty(sortColumn)){
            params.put("sortColumn", sortColumn);
        }else {
            params.put("sortColumn", "id");
        }
        if(!StringUtils.isEmpty(sortDir)){
            params.put("sortDir", sortDir);
        }else {
            params.put("sortDir", "asc");
        }
        if(!StringUtils.isEmpty(query)){
            query = "%"+query+"%";
        }else {
            query = "%0%";
        }
        params.put("query", query);
        params.put("administrator", false);
        return (PageList<People>)peopleMapper.selectList(params, new PageBounds(page, pageSize));
    }

    public List<People> selectAllPeople() {
        return peopleMapper.selectAllPeople();
    }

    public void savePeople(People people){
        if(people!=null){
            if(StringUtils.isEmpty(people.getId())) {
                people.setId(UUID.randomUUID().toString());
            }
            if(StringUtils.isEmpty(people.getFullname())){
                throw new PeopleException("名字不能为空");
            }
            if(StringUtils.isEmpty(people.getPhone())){

                throw new PeopleException("电话不能为空");
            }
//            if(StringUtils.isEmpty(people.getDoctorId())){
//                throw new PeopleException("医生信息不能为空");
//            }
//            if(StringUtils.isEmpty(String.valueOf(people.getBirthday()))){
//
//                throw new PeopleException("生日不能为空");
//            }
//            if(StringUtils.isEmpty(people.getCardId())){
//
//                throw new PeopleException("身份证不能为空");
//            }
//            if(StringUtils.isEmpty(String.valueOf(people.getGender()))){
//
//                throw new PeopleException("性别不能为空");
//            }
//            if(StringUtils.isEmpty(people.getAddress())){
//
//                throw new PeopleException("地址不能为空");
//            }
//            if(StringUtils.isEmpty(people.getRemark())){
//
//                throw new PeopleException("备注不能为空");
//            }
            peopleMapper.insert(people);
        }
        else {
            throw new PeopleException("人员信息为空");
        }
    }

    public People selectByPeopleId(String peopleId) {
        return peopleMapper.selectByPrimaryKey(peopleId);
    }

    public void deletePeople(String peopleId) {
        if(peopleId==null||peopleMapper.selectByPrimaryKey(peopleId)==null){
            throw new PeopleException("不存在此用户");
        }
        peopleMapper.deleteByPrimaryKey(peopleId);
    }

    public void updateByPrimaryKey(People people) {
        if(people!=null){
            if(StringUtils.isEmpty(people.getFullname())){
                throw new PeopleException("名字不能为空");
            }
            if(StringUtils.isEmpty(people.getPhone())){

                throw new PeopleException("电话不能为空");
            }
//            if(StringUtils.isEmpty(people.getDoctorId())){
//                throw new PeopleException("医生信息不能为空");
//            }
//            if(StringUtils.isEmpty(String.valueOf(people.getBirthday()))){
//
//                throw new PeopleException("生日不能为空");
//            }
//            if(StringUtils.isEmpty(people.getCardId())){
//
//                throw new PeopleException("身份证不能为空");
//            }
//            if(StringUtils.isEmpty(String.valueOf(people.getGender()))){
//
//                throw new PeopleException("性别不能为空");
//            }
//            if(StringUtils.isEmpty(people.getAddress())){
//
//                throw new PeopleException("地址不能为空");
//            }
//            if(StringUtils.isEmpty(people.getRemark())){
//
//                throw new PeopleException("备注不能为空");
//            }
        }
        else {
            throw new PeopleException("人员信息为空");
        }
        peopleMapper.updateByPrimaryKey(people);
    }


    public void savePeopleOrganizationMap(String peopleId, String organizationId) {
        if(peopleOrganizationMapMapper.selectByPeopleIdAndOrganizationId(peopleId, organizationId) == null){
            PeopleOrganizationMap peopleOrganizationMap = new PeopleOrganizationMap();
            if(StringUtils.isEmpty(organizationId)) {
                peopleOrganizationMap.setOrganizationId("6");
            }
            peopleOrganizationMap.setOrganizationId(organizationId);
            peopleOrganizationMap.setPeopleId(peopleId);
            peopleOrganizationMapMapper.insertPeopleOrganizationMap(peopleOrganizationMap);
        }

    }
    public void saveUserPeopleMap(String userId, String peopleId) {
//        if(userPeopleMapMapper.selectByUserIdAndPeopleId(userId, peopleId) == null){
//            UserPeopleMap userPeopleMap = new UserPeopleMap();
//            userPeopleMap.setUserId(userId);
//            userPeopleMap.setPeopleId(peopleId);
//            userPeopleMap.setInputDate(new Date());
//            userPeopleMapMapper.insertUserPeopleMap(userPeopleMap);
//        }

    }

    public List<People> selectByUserId(String userId) {
        return peopleMapper.selectByUserId(userId);
    }

    public void bindUser(String peopleId, String userId) {
        People people = selectByPeopleId(peopleId);
        if(people!=null){
            people.setUserId(userId);
            updateByPrimaryKey(people);
        } else {
            throw new PeopleException("人员信息为空");
        }
    }


    public void unbindUser(String peopleId) {
        People people = selectByPeopleId(peopleId);
        if(people!=null){
            people.setUserId("");
            updateByPrimaryKey(people);
        } else {
            throw new PeopleException("人员信息为空");
        }
    }


    public void bindUserPeople(String peopleId, String userId) {
        Map<String, Object> map = userPeopleMapper.selectByUserIdAndPeopleId(userId, peopleId);
        if(map == null){
            Map<String,Object> params = new HashMap<String,Object>();
            if(StringUtils.isEmpty(peopleId)){
                throw new PeopleException("peopleId是空值");
            }
            if(StringUtils.isEmpty(userId)){
                throw new PeopleException("userId是空值");
            }
            params.put("peopleId", peopleId);
            params.put("userId", userId);
            params.put("inputDate", new Date());
            userPeopleMapper.insert(params);
        }

    }


    public void unbindUserPeople(String peopleId, String userId) {
        Map<String,Object> params = new HashMap<String,Object>();
        if(StringUtils.isEmpty(peopleId)){
            throw new PeopleException("peopleId是空值");
        }
        if(StringUtils.isEmpty(userId)){
            throw new PeopleException("userId是空值");
        }
        params.put("peopleId", peopleId);
        params.put("userId", userId);
        userPeopleMapper.delete(params);
    }
}
